home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue99 / CPROG5.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-05  |  3.8 KB  |  109 lines

  1. /* CPROG5.CPP - SOME EXAMPLES OF IF AND IF/ELSE
  2.     Compile and run with Ctrl+F9, the press Alt+5 to see the output
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <dos.h>    // Contains prototype for the sound() and other
  8.             // functions used in the 'if (x == y)' case below.
  9. #include <stdlib.h>    // Contains the defintion of random() which is
  10.             // not a function, but a #define macro. Also
  11.             // contains the protype for randomize();
  12. #include <time.h>    // See help on randomize() for explanation
  13.  
  14.  
  15. main()
  16. {
  17. int x, y;        // Shorthand for int x; followed by int y;
  18.     randomize();    // Initialise the random number generator with a random number
  19.     clrscr();    // Clear the screen
  20.     x=random(100);    // Get random integers in the range 0-99 into
  21.     y=random(100);    // variables x and y. See help system for more
  22.             // info on random().
  23.  
  24.     printf("X is %d and Y is %d\n", x, y);
  25.             /* Note how that with two %d codes in the string
  26.                printf() expects two integers after it. You may
  27.                find it confusing that the text of the article
  28.                implies a strict definition of the number of
  29.                and type of parameters a function takes, but
  30.                printf() seems to be able to handle different
  31.                numbers and types of variable. There is a
  32.                mechanism whereby you can set up a function to
  33.                handle variable amounts and types of input, but
  34.                that is beyond the scope of this series. */
  35.  
  36.     delay(2000);    // Delay 2 seconds
  37.  
  38.     if ( x < y )
  39.         printf("X is smaller than Y\n");
  40.     else
  41.         printf("X is larger than or equal to Y\n");
  42.  
  43.             /* Following an IF with an ELSE gives alternative
  44.                courses of action, depending on the result of the
  45.                test. */
  46.  
  47.     delay(2000);
  48.     x=y;            // Force x=y so you can see the next bit
  49.                 // working first time round.
  50.     if ( x == y )
  51.         {
  52.         printf("Amazing - X and Y are both equal\n");
  53.         sound(700);    // Sound a 700Hz tone until further notice
  54.         delay(1000);    // Sit around doing nothing for a second
  55.         nosound();    // And turn off the sound
  56.         }
  57.             /* Several commands are executed if x and y are
  58.                equal. The bottom three were highlighted, copied
  59.                and pasted out of the example given under sound()
  60.                in the help system, then edited.
  61.                Remember - put the cursor on a function name or
  62.                C++ keyword, press shift+F1, and you are taken
  63.                to the appropriate entry in Help. */
  64.     delay(2000);
  65.     x=random(100);        // Change x so the next bit works...
  66.  
  67.                 /* Naughty bit coming up! For clear code
  68.                    #defines should be at the head of the
  69.                    program or grouped in a header file.
  70.                    There's nothing to stop you putting a
  71.                    #define down here, but it makes for messy
  72.                    code that's harder to debug. Don't do it! */
  73.  
  74. #define BEEP sound(200); delay(500); nosound();
  75.                 /* This #define makes subsequent uses
  76.                    of BEEP expand into everything that
  77.                    follows it on the same line. */
  78.  
  79.     if( x != y)
  80.         {
  81.         printf("Sorry, my mistake, they're different!\n");
  82.         BEEP
  83.         }
  84.     else
  85.         {
  86.         printf("I can confirm that they are still equal!\n");
  87.         BEEP
  88.         }
  89.                 /* A few things to note here...
  90.                 - Once again, braces are used to group
  91.                   several actions where, without them,
  92.                   only the first one would be executed.
  93.                 - Each instance of BEEP is expanded
  94.                   by the preprocessor into the three
  95.                   function calls listed in the #define line.
  96.                   Since ; marks the end of a command it is
  97.                   Ok to put several calls on one line.
  98.                 - BEEP hasn't got a semi-colon - the one from
  99.                   nosound(); does the job. Had that been
  100.                   missing, you would need to write BEEP;
  101.                 - If BEEP had been the only action after the
  102.                   IF or the ELSE, it would still need braces
  103.                   because it expands to three commands.
  104.                 - I tend to use capitals to indicate macro
  105.                   names.*/
  106.  
  107.     delay(5000);        // Wait 5 seconds
  108. }
  109.